package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.HospitalRepository;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.model.hosp.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    HospitalRepository hospitalRepository;

    @Override
    public void save(Map<String, Object> parmmap) {
//        把Map集合转换成对象
        String s = JSONObject.toJSONString(parmmap);
        Hospital hosp = JSONObject.parseObject(s, Hospital.class);
//        判断是否存在数据
        String hoscode = hosp.getHoscode();
        Hospital hospitalExist= hospitalRepository.getHospitalByHoscode(hoscode);
        if(hospitalExist!=null)
        {
            hosp.setStatus(hospitalExist.getStatus());
            hosp.setCreateTime(hospitalExist.getCreateTime());
            hosp.setUpdateTime(new Date());
            hosp.setIsDeleted(0);
            hospitalRepository.save(hosp);
        }else
        {
            hosp.setStatus(0);
            hosp.setCreateTime(new Date());
            hosp.setUpdateTime(new Date());
            hosp.setIsDeleted(0);
            hospitalRepository.save(hosp);
        }

    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hosp = hospitalRepository.getHospitalByHoscode(hoscode);
        System.out.println(hosp.toString());
        return hosp;
    }
}
